﻿@model object
@{
    Layout = ViewBag.Layout ?? "_TR.cshtml";
    ViewData.TemplateInfo.HtmlFieldPrefix = ViewData.TemplateInfo.HtmlFieldPrefix.Replace(ViewData.ModelMetadata.PropertyName, "").Trim('.');
    var propertyName = ViewData["name"] == null ? ViewData.ModelMetadata.PropertyName : ViewData["name"].ToString();
    var fieldId = ViewData.TemplateInfo.GetFullHtmlFieldId(propertyName);

    var editorHtmlAttributes = Html.GetUnobtrusiveValidationAttributes(propertyName, ViewData.ModelMetadata).Merge(ViewData.ModelMetadata.AdditionalValues, true);
    var htmlAttributes = ViewBag.HtmlAttributes;
    if (htmlAttributes != null)
    {
        if (htmlAttributes is IDictionary<string, object>)
        {
            editorHtmlAttributes.Merge((IDictionary<string, object>)htmlAttributes);
        }
        else
        {
            editorHtmlAttributes.Merge(new RouteValueDictionary(htmlAttributes));
        }
    }

    var fileInputName = propertyName + "_file";
    
   
}
<div id="@(fieldId + "_file")">
    <input type="file" name="@propertyName" @Html.GenerateHtmlAttributes(editorHtmlAttributes) @(new[] { Validator.Regex(".+\\.(zip)$", "Required a zip file.").ClientValidationRule }.GenerateHtmlAttributes()) accept=".zip"/>
    @if (ViewData.ModelMetadata.GetDataSource().GetSelectListItems(ViewContext.RequestContext).Count() > 0)
    {
        <a class="action" id="@(fieldId + "_select")" title="@("Switch".Localize())">@Html.IconImage("switch")</a>
    }
</div>

<div id="@(fieldId + "_dropdown")">
    @Html.DropDownList(propertyName, ViewData.ModelMetadata.GetDataSource()
                    .GetSelectListItems(ViewContext.RequestContext, "").SetActiveItem(Model), editorHtmlAttributes.Merge("disabled", "disabled").Merge("class", "medium"))
    <a class="action" id="@(fieldId + "_upload")" title="@("Switch".Localize())">@Html.IconImage("switch")</a>
</div>
@Html.ValidationMessage(ViewData.ModelMetadata, new { name = ViewData["name"] })
@if (!string.IsNullOrEmpty(ViewData.ModelMetadata.Description))
{
    <em class="tip">@Html.Raw(ViewData.ModelMetadata.Description.Localize())</em>
}
<script>
    $(function ($) {
        //$("select[name=@fieldId]").select2({ minimumResultsForSearch: 20 });
        var dropdownField = $('#@(fieldId + "_dropdown")');
        var fileField = $('#@(fieldId + "_file")');
        var select = $('#@(fieldId + "_select")');
        var upload = $('#@(fieldId + "_upload")');
        dropdownField.hide();
        fileField.show();
        select.click(function () {
            dropdownField.find('select').removeAttr('disabled');
            fileField.find('input').attr('disabled', 'disabled');

            fileField.hide();
            dropdownField.show();
        });
        upload.click(function () {
            fileField.find('input').removeAttr('disabled');
            dropdownField.find('select').attr('disabled', 'disabled');

            dropdownField.hide();
            fileField.show();
        });
    });
</script>
